MICROLITE_BENCHMARK_ROOT_DIR := $(TENSORFLOW_ROOT)tensorflow/lite/micro/tools/benchmarking

ifneq ($(GENERIC_BENCHMARK_MODEL_PATH),)
    GENERIC_BENCHMARK_MODEL_DIR := $(dir $(GENERIC_BENCHMARK_MODEL_PATH))
    GENERIC_BENCHMARK_MODEL_NAME := $(notdir $(basename $(GENERIC_BENCHMARK_MODEL_PATH)))
    CXXFLAGS += -DGENERIC_BENCHMARK_USING_BUILTIN_MODEL
    CXXFLAGS += -DGENERIC_BENCHMARK_MODEL_HEADER_PATH=\"$(GENERIC_BENCHMARK_MODEL_DIR)$(GENERIC_BENCHMARK_MODEL_NAME)_model_data.h\"
    CXXFLAGS += -DGENERIC_BENCHMARK_MODEL_NAME=$(GENERIC_BENCHMARK_MODEL_NAME)
ifneq ($(GENERIC_BENCHMARK_ARENA_SIZE),)
    CXXFLAGS += -DGENERIC_BENCHMARK_TENSOR_ARENA_SIZE=$(GENERIC_BENCHMARK_ARENA_SIZE)
endif

    # model path includes $(TENSORFLOW_ROOT) as part of the make invocation
    GENERIC_BENCHMARK_GENERATOR_INPUTS := $(GENERIC_BENCHMARK_MODEL_PATH)

    GENERIC_BENCHMARK_GENERATED_SRCS := \
    $(GENERATED_SRCS_DIR)$(GENERIC_BENCHMARK_MODEL_DIR)$(GENERIC_BENCHMARK_MODEL_NAME)_model_data.cc

    GENERIC_BENCHMARK_GENERATED_HDRS := \
    $(GENERATED_SRCS_DIR)$(GENERIC_BENCHMARK_MODEL_DIR)$(GENERIC_BENCHMARK_MODEL_NAME)_model_data.h
endif

GENERIC_BENCHMARK_SRCS := \
$(MICROLITE_BENCHMARK_ROOT_DIR)/generic_model_benchmark.cc \
$(MICROLITE_BENCHMARK_ROOT_DIR)/metrics.cc \
$(GENERATED_SRCS_DIR)$(MICROLITE_BENCHMARK_ROOT_DIR)/show_meta_data.cc

GENERIC_BENCHMARK_HDRS := \
$(MICROLITE_BENCHMARK_ROOT_DIR)/op_resolver.h \
$(MICROLITE_BENCHMARK_ROOT_DIR)/metrics.h \
$(MICROLITE_BENCHMARK_ROOT_DIR)/show_meta_data.h

# always rebuild these to catch MODEL_PATH and ARENA_SIZE changes on command line
.PHONY: $(GENERATED_SRCS_DIR)$(MICROLITE_BENCHMARK_ROOT_DIR)/show_meta_data.cc
.PHONY: $(MICROLITE_BENCHMARK_ROOT_DIR)/generic_model_benchmark.cc

$(GENERATED_SRCS_DIR)$(MICROLITE_BENCHMARK_ROOT_DIR)/show_meta_data.cc:
	CC="$(CC)" \
	CXX="$(CXX)" \
	CC_FLAGS="$(CCFLAGS)" \
	CXX_FLAGS="$(CXXFLAGS)" \
	KERNEL_OPTIMIZATION="$(KERNEL_OPTIMIZATION_LEVEL)" \
	CORE_OPTIMIZATION="$(CORE_OPTIMIZATION_LEVEL)" \
	THIRD_PARTY_KERNEL_OPTIMIZATION="$(THIRD_PARTY_KERNEL_OPTIMIZATION_LEVEL)" \
	TARGET=$(TARGET) \
	TARGET_ARCH=$(TARGET_ARCH) \
	TENSORFLOW_ROOT="$(TENSORFLOW_ROOT)" \
	OPTIMIZED_KERNEL=$(OPTIMIZED_KERNEL_DIR) \
	BUILD_TYPE=$(BUILD_TYPE) \
	XTENSA_CORE=$(XTENSA_CORE) \
	XTENSA_BASE=$(XTENSA_BASE) \
	XTENSA_TOOLS_VERSION=$(XTENSA_TOOLS_VERSION) \
	TEMPLATE_FILE="$(MICROLITE_BENCHMARK_ROOT_DIR)/show_meta_data.cc.template" \
	GENERATED_FILE="$(GENERATED_SRCS_DIR)$(MICROLITE_BENCHMARK_ROOT_DIR)/show_meta_data.cc" \
	MODEL_FILE="$(GENERIC_BENCHMARK_MODEL_PATH)" \
	$(MICROLITE_BENCHMARK_ROOT_DIR)/collect_meta_data.sh

ifneq ($(TARGET),bluepill)
ifneq ($(TARGET_ARCH), $(filter $(TARGET_ARCH), hifimini))
    $(eval $(call microlite_test,tflm_benchmark,\
    $(GENERIC_BENCHMARK_SRCS),$(GENERIC_BENCHMARK_HDRS),\
    $(GENERIC_BENCHMARK_GENERATOR_INPUTS)))
endif
endif
